;define(function(){
	
	var navigationMenu;
	if(navigationMenu)
	{
		navigationMenu = null;
	}

	navigationMenu = {
		init: function()
		{
			navigationMenu.getNavigationMenu("0");
			navigationMenu.bindEvents();
			//TODO  临时添加用户名   以后考虑   在模板中 添加图标  
			$('#side-menu .nhu-name span').text(user.name);
		},
		init2: function()
		{
			navigationMenu.getNavigationMenu("1");
			navigationMenu.bindEvents();
			//TODO  临时添加用户名   以后考虑   在模板中 添加图标  
			$('#side-menu .nhu-name span').text(user.name);
		},
		bindEvents: function()
		{
			//签到栏
			$(".nht-ul li .nht-ul-d").unbind().bind({
				mouseenter: function(){
					$(this).find(".nus-hexagon").addClass("nus-hexagon-effect");
				},
				mouseleave: function(){
					$(this).find(".nus-hexagon").removeClass("nus-hexagon-effect");
				}
			});
			
			//状态筛选
			$(".task-i-h-ul li span").on("click", function(){
				$(".task-i-h-ul li span").removeClass("i-h-choose");
				$(this).addClass("i-h-choose");
				
				var state = $(this).attr("state");
				var $ul = $(this).closest(".task-info").find(".task-info-list .task-i-l-ul");
				$ul.find("li").show();
				
				if(state != "")
				{
					$ul.find("li").not($ul.find("li[state='0']")).hide();
				}
			});
			
			//任务链表
			$(".task-i-l-ul li i").on("click", function(e){
				var proid = $(this).closest("li").attr("productid");
				if(proid == "YZ_YJGB"){
					e.stopPropagation();
					e.stopImmediatePropagation();
					window.open("http://10.138.175.140:8080/broadcast/");
					return;
				}
				$(".task-i-l-ul li").removeClass("i-l-ul-li");
				$(".task-i-l-ul li").find(".task-s-active").removeClass("task-s-a-choose");
				$(this).parent().addClass("i-l-ul-li");
				$(this).parent().find(".task-s-active").addClass("task-s-a-choose");
				//不定时任务
				if($(".tf-system .bds-task li").hasClass("i-l-ul-li")){	
					$(".tf-system .bds-task li").removeClass("i-l-ul-li");
				}
			});
			
			//不定时任务
			$(".tf-system .bds-task li").on("click", function(e){
				e.stopImmediatePropagation();
				var proid = $(this).attr("productid");
				if(!$(this).hasClass("i-l-ul-li")){
					$(this).addClass("i-l-ul-li");
					$(this).siblings().removeClass("i-l-ul-li");
					$(this).find("i.cafe-sider-i").trigger("click");
				}else{
					$(this).find("i.cafe-sider-i").trigger("click");
				}
			});
			
			//任务链表向下展开
			// navigationMenu.xxBindEvents();
			
			//去蓝底
			$(".nav").on("selectstart",function(){
				return false;
			});
			
		},
		
		//任务链表向下展开
		// xxBindEvents: function()
		// {
		// 	$(".task-menu").unbind().bind("click", function(){
		//
		// 		$(".task-menu").closest("li").find(".task-info").hide();
		// 		$(this).closest("li").find(".task-info").show();
		// 		$(".task-menu").find(".task-arrow").removeClass("cafe-ic-xiangshang1");
		// 		$(".task-menu").find(".task-arrow").addClass("cafe-ic-xiala1");
		//
		// 		$(this).closest("li").find(".task-info").show();
		// 		$(this).find(".task-arrow").removeClass("cafe-ic-xiala1");
		// 		$(this).find(".task-arrow").addClass("cafe-ic-xiangshang1");
		// 	});
		// },
		
		//获取菜单信息
		getNavigationMenu: function(type)
		{
			$.ajax({
				type:'POST',
				url: window.contextPath + 'main/getNavigationMenu.do?type='+type,
				dataType:"json",
				async:false,
				success:function(result)
				{
					navigationMenu.nmInfo = result.params.nmInfo;
					
					navigationMenu.taskSSH = []; //每日任务
					navigationMenu.taskUTH = []; //不定时任务
					navigationMenu.taskPDH = []; //周期性任务	
					
					
					//后台传过来的所有任务区分开
					if(navigationMenu.nmInfo && navigationMenu.nmInfo.length > 0)
					{
						for(var i = 0; i < navigationMenu.nmInfo.length; i++)
						{
							var obj = navigationMenu.nmInfo[i];
							if(obj)
							{
								if(obj.taskType == 0)
								{
									//每日任务
									navigationMenu.taskSSH.push(obj);
								}
								else if(obj.taskType == 1)
								{
									//不定时任务
									navigationMenu.taskUTH.push(obj);
								}
								else if(obj.taskType == 2)
								{
									//周期性任务
									//navigationMenu.taskPDH.push(obj);
								}
							}
						}
					}
					
					//把区分后的任务按照时间升序排序
					//每日任务
					if(user.codeId != "330211"){
						if(navigationMenu.taskSSH.length > 0)
						{
							var temp = null;
							for(var i = 0; i < navigationMenu.taskSSH.length; i++)
							{
								for(var j = i + 1; j < navigationMenu.taskSSH.length; j++)
								{
									if(navigationMenu.taskSSH[i].startDatetime > navigationMenu.taskSSH[j].startDatetime)
									{
										temp = navigationMenu.taskSSH[i];
										navigationMenu.taskSSH[i] = navigationMenu.taskSSH[j];
										navigationMenu.taskSSH[j] = temp;
									}
								}
							}
						}
					}
					
					//不定时任务
					if(navigationMenu.taskUTH.length > 0)
					{
						var temp = null;
						for(var i = 0; i < navigationMenu.taskUTH.length; i++)
						{
							for(var j = i + 1; j < navigationMenu.taskUTH.length; j++)
							{
								if(navigationMenu.taskUTH[i].startDatetime > navigationMenu.taskUTH[j].startDatetime)
								{
									temp = navigationMenu.taskUTH[i];
									navigationMenu.taskUTH[i] = navigationMenu.taskUTH[j];
									navigationMenu.taskUTH[j] = temp;
								}
							}
						}
					}
					
					//周期性任务
					if(navigationMenu.taskPDH.length > 0)
					{
						var temp = null;
						for(var i = 0; i < navigationMenu.taskPDH.length; i++)
						{
							for(var j = i + 1; j < navigationMenu.taskPDH.length; j++)
							{
								if(navigationMenu.taskPDH[i].startDatetime > navigationMenu.taskPDH[j].startDatetime)
								{
									temp = navigationMenu.taskPDH[i];
									navigationMenu.taskPDH[i] = navigationMenu.taskPDH[j];
									navigationMenu.taskPDH[j] = temp;
								}
							}
						}
					}
					
					navigationMenu.getMontage();
				}
			});
		},
		
		//拼接菜单
		getMontage: function()
		{
			//每日任务
			if(navigationMenu.taskSSH.length > 0)
			{
				$(".nav-ssh .task-i-l-ul").empty();
				
				var state = 0;
				
				for(var i = 0; i < navigationMenu.taskSSH.length; i++)
				{
					var data = navigationMenu.taskSSH[i];
					if(data.taskState == 0 && data.isLate == 0){
						state = 0;
					}else if(data.taskState == 0 && data.isLate == 1){
						state = 2;
					}else if(data.taskState == 1 && data.isLate == 0){
						state = 1;
					}else if(data.taskState == 1 && data.isLate == 1){
						state = 1;
					}
					var time = navigationMenu.getTime(data.startDatetime);
					var stateStyle = navigationMenu.getStateStyle(state);
					var $li = null;
					var url = "ningbo/forecastView/initContent.do";
					
					$li = $('<li islate="'+ data.isLate +'" state="' + data.taskState + '" productId='+data.productId+' style="display: '+ (data.taskState == 0 ? "block" : "none") +';">'
						  + '	<span class="task-s-active"></span>'
							  + '	<span class="task-s-time ' + stateStyle + '" dataTime="'+new Date(data.startDatetime)+'" proId="'+data.productId+'" pubTime="'+new Date(data.publishDatetime)+'">' + time.hours + ":" + time.minutes + '</span>'
						  + '	<i class="task-s-name cafe-sider-i" data-href="'+url+'" data-target="_self" data-ns="'+data.namespace+'">' + data.taskName + '</i>'
						  + '</li>'
					);
					
					$(".nav-ssh .task-i-l-ul").append($li);
				}
				
			}
			
			//不定时任务
			if(navigationMenu.taskUTH.length > 0)
			{
				$(".nav-uth .task-i-l-ul").empty();
				
				for(var i = 0; i < navigationMenu.taskUTH.length; i++)
				{
					var data = navigationMenu.taskUTH[i];
					var time = navigationMenu.getTime(data.startDatetime);
					var stateStyle = navigationMenu.getStateStyle(data.taskState);
					var url = "ningbo/forecastView/initContent.do";
					if(data.namespace == "nb_dxfb_forecast"){
						url = "bdsrw/init.do?type=dx";
					}else if(data.namespace == "nb_czfb_forecast"){
						url = "bdsrw/init.do?type=cz";
					}else if(data.namespace == "nb_yjfb_forecast"){
						url = "bdsrw/init.do?type=yj";
					}
//					var $li = null;
//					$li = $('<li state="' + data.taskState + '" productId='+data.productId+'>'
//						  + '	<span class="task-s-active"></span>'
//						  + '	<span class="task-s-time ' + stateStyle + '"></span>'
//						  + '	<i class="task-s-name cafe-sider-i" data-href="'+url+'" data-target="_self" data-ns="'+data.namespace+'">&emsp;&emsp;&emsp;' + data.taskName + '</i>'
//						  + '</li>'
//					);
//					$(".nav-uth .task-i-l-ul").append($li);
					var $li = null;
					$li = $('<li state="' + data.taskState + '" productId='+data.productId+'>'
						  + '	<span class="task-s-active"></span>'
						  + '	<span class="task-s-time ' + stateStyle + '"></span>'
						  + '	<i class="task-s-name cafe-sider-i" data-href="'+url+'" data-target="_self" data-ns="'+data.namespace+'">&emsp;&emsp;&emsp;' + data.taskName + '</i>'
						  + '   <div class="useThis">' + data.taskName + '</div>'
						  + '</li>'
					);
					if(Number(i)%2==0){
						$li.addClass('signal');
					}
					$(".tf-system .bds-task").append($li);
				}
			}
			
			//周期性任务
			if(navigationMenu.taskPDH.length > 0)
			{
				$(".nav-pdh .task-i-l-ul").empty();
				
				for(var i = 0; i < navigationMenu.taskPDH.length; i++)
				{
					var data = navigationMenu.taskPDH[i];
					var time = navigationMenu.getTime(data.startDatetime);
					var stateStyle = navigationMenu.getStateStyle(data.taskState);
					var $li = null;
					var url = "ningbo/forecastView/initContent.do";
					$li = $('<li state="' + data.taskState + '" productId='+data.productId+'>'
						  + '	<span class="task-s-active"></span>'
						  + '	<span proId="'+data.productId+'" class="task-s-time ' + stateStyle + '"></span>'
						  + '	<i class="task-s-name cafe-sider-i" data-href="'+url+'" data-target="_self" data-ns="'+data.namespace+'">' + data.taskName + '</i>'
						  + '</li>'
					);
					
					$(".nav-pdh .task-i-l-ul").append($li);
				}
			}
			
		},
		
		//获取时间
		getTime: function(time)
		{
			var result = {"year": "", "month": "", "day": "", "hours": "", "minutes": ""};
			
			if(time)
			{
				var date = new Date(time);
				var year = date.getFullYear();
				var month = date.getMonth() + 1;
				var day = date.getDate();
				var hours = date.getHours();
				var minutes = date.getMinutes();
				
				if(month < 10)
				{
					month = "0" + month;
				}
				
				if(day < 10)
				{
					day = "0" + day;
				}
				
				if(hours < 10)
				{
					hours = "0" + hours;
				}
				
				if(minutes < 10)
				{
					minutes = "0" + minutes;
				}
				
				result = {"year": year, "month": month, "day": day, "hours": hours, "minutes": minutes};
			}
			
			return result;
		},
		
		//为任务状态添加样式
		getStateStyle: function(state)
		{
			var result = "";
			
			if(state)
			{
				if(state == 1)
				{
					result = "task-info-accomplish";
				}
				else if(state == 2)
				{
					result = "task-info-overtime";
				}
			}
			
			return result;
		}
	}
	
	var obj = {
		init: navigationMenu.init,
		refreshEvs: navigationMenu.bindEvents
	};
	
	var obj2 = {
			init2: navigationMenu.init2,
			refreshEvs: navigationMenu.bindEvents
		};
	
	CafeJS.navMenu = function(){
		obj.init();
		obj.refreshEvs();
		
		/*var call = {
				ref: function(){},
		};*/
		
		return obj;
	};
	
	CafeJS.navMenu2 = function(){
		obj2.init2();
		obj2.refreshEvs();
		
		/*var call = {
				ref: function(){},
		};*/
		
		return obj2;
	};
});